package de.unibi.techfak.msp.eatThis.service;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import de.unibi.techfak.msp.eatThis.bean.FoodBean;
import de.unibi.techfak.msp.eatThis.manager.FoodManager;
import de.unibi.techfak.msp.eatThis.messages.Messages;
import de.unibi.techfak.msp.eatThis.manager.exception.EntityException;
import static de.unibi.techfak.msp.eatThis.util.Parameters.NAME;
import static de.unibi.techfak.msp.eatThis.util.Parameters.EAN;
import static de.unibi.techfak.msp.eatThis.util.Parameters.VALUE_TRUE;
import static de.unibi.techfak.msp.eatThis.util.Parameters.LISTED;

/**
 * Servlet implementation class FoodServlet
 */
@WebServlet(description = "Lebensmittel abholen!", urlPatterns = {
        "/FoodServlet", "/food" })
public class FoodServlet extends DefaultService {
    
    /** Id. */
    private static final long serialVersionUID = 1L;
    
    /** FoodBean. */
    private FoodBean fb;
    
    /** FoodManager. */
    private FoodManager fm;
    
    /** FoodList. */
    private List<FoodBean> foodList;
    
    @Override
    public void init() throws ServletException {
        super.init();

        // prepare Manager and return bean
        fm = new FoodManager();
        fb = new FoodBean();
    }
    
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FoodServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");
        
        String param = null;
        String ean = null;
        fb = null;
        
        try {
            param = request.getParameter(NAME);
            ean = request.getParameter(EAN);
            
            if (param != null) {
                fb = fm.getByName(param);
            } else if (ean != null) {
                fb = fm.getByEAN(ean);
            } else if (VALUE_TRUE.equals(request.getParameter(LISTED))) {
                foodList = fm.getFoodList();
                response.getWriter().write(jettionise(foodList));
                response.setStatus(HttpServletResponse.SC_OK);
                return;
            } else {
                response.sendError(HttpServletResponse.SC_NOT_FOUND,
                        Messages.EXCEPTION_ATTRIBUTE_NOT_FOUND);
                return;
            }
        } catch (EntityException e) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND,
                    e.getMessage());
            return;
        }
        
        response.getWriter().write(jettionise(fb));
        response.setStatus(HttpServletResponse.SC_OK);
        
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
     *      response)
     */
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        
        response.setContentType("text/html;charset=UTF-8");
        
        fb = (FoodBean) deJettionise(readJsonInputFromStream(request), FoodBean.class);
        
        if (fb == null) {
            response.setStatus(HttpServletResponse.SC_CONFLICT);
            response.sendError(HttpServletResponse.SC_CONFLICT, "Bean null!");
            return;
        }
        
        if (fb.getId() == null) {
            if (fm.create(fb) != null) {
                response.setStatus(HttpServletResponse.SC_OK);
            } else {
                response.setStatus(HttpServletResponse.SC_CONFLICT);
                response.sendError(HttpServletResponse.SC_CONFLICT, "Create null!");
                return;
            }
        } else {
            response.setStatus(HttpServletResponse.SC_CONFLICT);
            response.sendError(HttpServletResponse.SC_CONFLICT, "Bean hat id!");
            return;
        }
        
        response.getWriter().write(jettionise(fb));
        response.setStatus(HttpServletResponse.SC_OK);
        
    }

}
